package com.example.sugar_demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.example.sugar_demo.bean.Exam;
import com.example.sugar_demo.bean.Paper;
import com.example.sugar_demo.bean.KeywordCount;
import com.example.sugar_demo.bean.Province;
import com.example.sugar_demo.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;



@RestController
public class SugarController {
    @Autowired
    TradeService tradeService;

    @RequestMapping("/sugar/keyword")
    public String keyword(Integer date){
        if (date == null){
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        List<KeywordCount> keywordCounts = tradeService.keywordCount(date);
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONArray data = new JSONArray();
        for (KeywordCount keyword : keywordCounts) {
            JSONObject obj = new JSONObject();
            obj.put("name",keyword.getKeyword());
            obj.put("value",keyword.getKeyword_count());
            data.add(obj);
        }
        result.put("data",data);
        return result.toJSONString();
    }

    @RequestMapping("/sugar/userRegister")
    public String userRegister(Integer date){
        if (date == null){
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        BigDecimal data = tradeService.getUserRegister(date);
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");
        result.put("data",data);
        return result.toJSONString();
    }


    //地图
    @RequestMapping("/sugar/province")
    public String province(Integer date) {
        if (date == null) {
            // 如果没有传入日期, 则使用当天做完查询的日期
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        List<Province> list = tradeService.province(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        data.put("valueName", "下单金额");
        
        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("订单数");
        data.put("tooltipNames", tooltipNames);
        
        JSONArray tooltipUnits = new JSONArray();
        tooltipUnits.add("个");
        
        data.put("tooltipUnits", tooltipUnits);
    
        JSONArray mapData = new JSONArray();
    
        for (Province province : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", province.getProvince_name());
            obj.put("value", province.getOrder_amount());
    
    
            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(province.getOrder_count());
            obj.put("tooltipValues", tooltipValues);
            mapData.add(obj);
        }
    
        data.put("mapData", mapData);
    
        result.put("data", data);
        
        
        return result.toJSONString();
    }

    //多指标柱形图
    @RequestMapping("/sugar/exam")
    public String exam(Integer date, Integer limit) {
        if (date == null) {
            // 如果没有传入日期, 则使用当天做完查询的日期
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        if (limit == null) {
            limit = 5;
        }

        List<Exam> list = tradeService.exam(date, limit);

        JSONObject result = new JSONObject();

        result.put("status", 0);
        result.put("msg", "");

        JSONObject data = new JSONObject();
        JSONArray categories = new JSONArray();
        for (Exam exam : list) {
            categories.add(exam.getCourse_name());
        }

        data.put("categories", categories);


        JSONArray series = new JSONArray();

        JSONObject obj = new JSONObject();
        obj.put("name", "人数 uv_ct");

        JSONArray data1 = new JSONArray();
        for (Exam exam : list) {
            data1.add(exam.getUv_ct());
        }
        obj.put("data", data1);

        JSONObject obj2 = new JSONObject();
        obj2.put("name", "分数 score_ct");

        JSONArray data2 = new JSONArray();
        for (Exam exam : list) {
            data2.add(exam.getScore_ct());
        }
        obj2.put("data", data2);

        JSONObject obj3 = new JSONObject();
        obj3.put("name", "做题时间 duration_sec_ct");

        JSONArray data3 = new JSONArray();
        for (Exam exam : list) {
            data3.add(exam.getDuration_sec_ct());
        }
        obj3.put("data", data3);

        series.add(obj);
        series.add(obj2);
        series.add(obj3);

        data.put("series", series);


        result.put("data", data);


        return result.toJSONString();
    }

    //单指标柱形图
    @RequestMapping("/sugar/paper")
    public String paper(Integer date, Integer limit) {
        if (date == null) {
            // 如果没有传入日期, 则使用当天做完查询的日期
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        if (limit == null) {
            limit = 5;
        }


        List<Paper> list = tradeService.paper(date,limit);

        JSONObject result = new JSONObject();

        result.put("status", 0);
        result.put("msg", "");

        JSONObject data = new JSONObject();
        JSONArray categories = new JSONArray();
        for (Paper paper : list) {
            categories.add(paper.getPaper_title());
        }

        data.put("categories", categories);


        JSONArray series = new JSONArray();

        JSONObject obj = new JSONObject();
        obj.put("name", "试卷 paper_title");

        JSONArray data1 = new JSONArray();
        for (Paper paper : list) {
            data1.add(paper.getUv_ct());
        }
        obj.put("data", data1);

        series.add(obj);


        data.put("series", series);


        result.put("data", data);


        return result.toJSONString();
    }
}

